﻿
namespace ClientAPI
{
    class CommandProtocol
    {
        private static CommandProtocol _protocolInstance = null;
        private bool _loggedIn = false;
        private bool _replied = false;
        private bool _isAuthor = false;
        private string _userName;
        private NetworkController _network;       

        // singleton instance of the protocol

        public static CommandProtocol Instance
        {
            get
            {
                if (_protocolInstance == null)
                    _protocolInstance = new CommandProtocol();
                return _protocolInstance;
            }
        }

        //sets the network controller
        public void SetNetworkController(NetworkController controller)
        {
            _network = controller;
        }

        // returns true if loggin
      
        public bool IsLoggedIn()
        {
            return _loggedIn;
        }

        // sends the login request
        //$FRANK,USER,LOGIN,username,password,

        public void Login(string userName, string password)
        {
            string message = "$FRANK,USER,LOGIN," + userName + "," + password + ",";
            _network.SendCommandMessage(message);
            _userName = userName;
        }

        //sends create user request
        //$FRANK,USER,CREATE,username,firstname,lastname,password

        public void CreateUser(string username, string fname, string lname, string pass, string phoneNum, string email)
        {
            string message = "$FRANK,USER,CREATE," + username + "," + fname + "," + lname + "," + pass + "," + phoneNum + "," + email + ",";
            _network.SendCommandMessage(message);
        }

        //sends disconnect request and clears username in cache
        //$FRANK,CLIENT,DISCONNECT,username,

        public void Disconnect()
        {
            _network.DisconnectChatHandler();
            string message = "$FRANK,CLIENT,DISCONNECT," + _userName + ",";
            _network.SendCommandMessage(message);
            _userName = null;
            _isAuthor = false;
            _network.Disconnect();
        }

        public bool IsAuthor()
        {
            return _isAuthor;
        }

        public string GetUserName()
        {
            return _userName;
        }

        public bool WaitForLoginReply()
        {
            while (!Instance._replied) ;
            _replied = false;
            return _loggedIn;            
        }

        public void MessageHandler(string message)
        {
            string[] messageArray = message.Split(',');
            if (messageArray[0] == "$FRANK")
            switch (messageArray[1])
            {
                case "CLIENT":
                    break;
                // recieves user login details
                case "USER":
                    switch(messageArray[messageArray.Length-2])
                    {
                        case "OK":
                            if (messageArray[messageArray.Length - 3] == "AUTHOR")
                                _isAuthor = true;
                            _loggedIn = true;
                            _replied = true;
                            break;
                        case "BAD":
                            _loggedIn = false;
                            _replied = true;
                            break;
                    }
                    break;
            }
            else System.Diagnostics.Debug.WriteLine("WE BEEN HACKED!!!");
        }
    }
}
